package com.kgc.scd.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

/**
 * Created On : 2022/6/10.
 * <p>
 * Author     : nainai
 * <p>
 * Description: 日期工具类
 */
@Slf4j
public class DateUtil {

    private static final SimpleDateFormat formatDay = new SimpleDateFormat("yyyy-MM-dd");

    private static final SimpleDateFormat formatMonth = new SimpleDateFormat("yyyy-MM");

    private static final SimpleDateFormat formatTime = new SimpleDateFormat("yyyy-MM-dd HH:mm-ss");

    /**
     * @author : nainai
     * @date   : 2022/6/10
     * @param  : [java.util.Date, java.util.Date]
     * @return : boolean
     * @description : 判断两个日期是否为连续的天，并且不夸月，传参日期单位天
     */
    public static boolean checkDateConsecutive(Date befDate, Date aftDate) {
        // 判断是否同年同月
        String s1 = formatMonth.format(befDate);
        String s2 = formatMonth.format(aftDate);
        if (!s1.equals(s2)){
            return false;
        }

        try {
            Date d1 = formatDay.parse(formatDay.format(befDate));
            Date d2 = formatDay.parse(formatDay.format(aftDate));
            return Math.abs(d1.getTime() - d2.getTime()) <= 24 * 60 * 60 * 1000;
        } catch (ParseException e) {
            log.error("日期格式转换异常", e.getMessage());
        }
        return false;
    }

    /**
     * @author : nainai
     * @date   : 2022/6/11
     * @param  : [java.util.Date, java.util.Date]
     * @return : boolean
     * @description : 判断是否为同一天，不看时间
     */
    public static boolean checkTheSameDay(Date date1, Date date2){
        String s1 = formatDay.format(date1);
        String s2 = formatDay.format(date2);
        if (!s1.equals(s2)){
            return false;
        }
        return true;
    }

    /**
     * @author :  
     * @date   : 2022/6/13
     * @param  : [java.util.Date, java.lang.Integer]
     * @return : boolean
     * @description : 传入领取时间，有效时间，判断是否到期，到期返回true 
     */
    public static boolean checkCouponPast(Date getDate, Integer valid) {
        try {
            getDate = formatTime.parse(formatTime.format(getDate));
            long pastTime = getDate.getTime() + 24 * 60 * 60 * 1000 * valid;
            // 当前时间
            Date date = formatDay.parse(formatDay.format(new Date()));
            // 到期时间
            Date date1 = new Date(pastTime);

            log.info("查询优惠券是否到期，当前时间：{}，到期时间：{}", date, date1);

            return date.after(date1);
        } catch (ParseException e) {
            log.error("日期格式转换异常", e.getMessage());
        }
        return false;
    }

    public static Date checkTimePast(Date getDate, Integer valid) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String minDateStr = "";
        Calendar calc =Calendar.getInstance();
        calc.setTime(getDate);
        calc.add(calc.DATE, +30);
        Date minDate = calc.getTime();
        minDateStr = sdf.format(minDate);
        System.out.println("minDateStr:"+minDateStr);
        return minDate;

    }



}